package com.lzxuni.modules.enterprise;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.enterprise.entity.EnterpriseInfo;
import com.lzxuni.modules.enterprise.service.EnterpriseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  企业信息 前端控制器
 * </p>
 *
 * @author liyl
 * @since 2019-06-27
 */
@RestController
@RequestMapping("/EnterpriseManage/EnterpriseInfo")
public class EnterpriseInfoController extends BaseController {

    @Autowired
    private EnterpriseInfoService enterpriseInfoService;

    @Autowired
    private FileEntityService fileEntityService;

    @RequestMapping("/Index")
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("/EnterpriseManage/EnterpriseInfo/index");
        System.out.println("登录用户:" +  getUser().getUsername());
        int temp = 0;
        if ("system".equals(getUser().getUsername())){
            temp = 1;
        }else{
            temp = 2;
        }
        mv.addObject("temp",temp);
        return mv;
    }

    //数据查询分页展示、模糊查询
    @RequestMapping("/GetList")
    public Object GetList(String pagination, EnterpriseInfo enterpriseInfo, String Keyword)throws SQLException {
        PageParameter pageParameter = JSON.parseObject(pagination,PageParameter.class);
        enterpriseInfo.setEnterpriseName(Keyword);
        PageData pageData = getPageData(enterpriseInfoService.queryPage(pageParameter,enterpriseInfo));
        return R.ok().put("data",pageData);
    }

    @RequestMapping("/Form")
    public ModelAndView insert(String keyValue) throws Exception {
        System.out.println(keyValue);
        ModelAndView mv = new ModelAndView("/EnterpriseManage/EnterpriseInfo/form");
        return mv;
    }

    //添加、修改
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue, String demopics, EnterpriseInfo enterpriseInfo,FileEntity fileEntity) throws Exception {
        if (StringUtils.isEmpty(keyValue)) {
            System.out.println(demopics);
            String ywId = UuidUtil.get32UUID();
            enterpriseInfo.setCreateTime(new Date());
            enterpriseInfo.setYwId(ywId);
            enterpriseInfoService.save(enterpriseInfo);
            if(!StringUtils.isEmpty(demopics) && !"&amp;nbsp;".equals(demopics)) {

                //enterpriseInfo.setEnterprisePictures(fileEntity.getUrlPath());
                System.out.println("新增yeId = " + ywId);
                fileEntityService.insert(demopics.replace("&quot;", "\""), ywId, "tp", "demopic", null);
            }
            return R.ok("保存成功");
        } else {

            fileEntityService.deleteByYwId(keyValue);
            enterpriseInfoService.updateById(enterpriseInfo);
            if(!StringUtils.isEmpty(demopics) && !"&amp;nbsp;".equals(demopics)) {
                fileEntityService.insert(demopics.replace("&quot;", "\""), keyValue, "tp", "demopic", null);
                enterpriseInfo.setYwId(keyValue);
            }
            return R.ok("修改成功");
        }
    }

    @RequestMapping("/Update")
    public ModelAndView update(String ywId,String keyValue) throws Exception{
        ModelAndView mv = new ModelAndView("/EnterpriseManage/EnterpriseInfo/Update");
        FileEntity fileBeanCustom = new FileEntity();
        fileBeanCustom.setYwId(ywId);
        fileBeanCustom.setYwType("demopic");
        mv.addObject("picNum", fileEntityService.queryNumByFileEntity(fileBeanCustom));
        fileBeanCustom.setYwType("demofile");
        mv.addObject("fileNum", fileEntityService.queryNumByFileEntity(fileBeanCustom));
        List<FileEntity> fileEntities = fileEntityService.queryListByFileEntity(fileBeanCustom);
        System.out.println("=========:"+StringUtils.isBlank(fileEntities));
        if(!fileEntities.isEmpty()){
            for(FileEntity fileEntity : fileEntities){
                mv.addObject("videoFile", fileEntity);
            }
        }else{
            mv.addObject("videoFile", "");
        }
        return mv;
    }

    //审批
    @RequestMapping("/ApprovalForm")
    public ModelAndView approval(String keyValue) throws Exception {
        ModelAndView mv = new ModelAndView("/EnterpriseManage/EnterpriseInfo/approvalform");
        mv.addObject("username",getUser().getRealName());
        return mv;
    }

    @RequestMapping("/SaveApproval")
    public Object approvalDo(String keyValue, EnterpriseInfo enterpriseInfo) throws Exception{
        enterpriseInfo.setYwId(keyValue);
        enterpriseInfoService.updateById(enterpriseInfo);
        return R.ok("审批成功");
    }

    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        enterpriseInfoService.removeById(keyValue);
        fileEntityService.deleteByYwId(keyValue);
        return R.ok("删除成功");
    }

}

